package com.leyou.item.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.services.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 品牌管理业务
 * @Date 2018/12/12  13:56
 * @Author JIE
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    /**
     * 品牌条件分页排序查询
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @Override
    public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        //分页
        PageHelper.startPage(page,rows);
        //创建条件查询
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)){
            //过滤条件
            example.createCriteria().orLike("name","%"+key+"%")
                    .orEqualTo("letter" +
                            "",key.toUpperCase());
        }
        //排序
        if(StringUtils.isNotBlank(sortBy)){
            String orderByClause=sortBy+(desc?" DESC" :" ASC");
            example.setOrderByClause(orderByClause);
        }
        //查询
        List<Brand> brands = brandMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnums.BREAND_NOT_FOND);
        }
        //解析分页结果
        PageInfo<Brand> info = new PageInfo<>(brands);
        //封装分页结果返回数据
        return new PageResult<>(info.getTotal(),brands);
    }

    /**
     * 新增品牌
     * @param brand
     * @param cids
     */

    @Override
    @Transactional
    public void saveBrand(Brand brand, List<Long> cids) {
        brand.setId(null);
        int count = brandMapper.insert(brand);
        if(count!=1){
            throw new LyException(ExceptionEnums.BREAND_SAVE_ERROR);
        }
        //新增中间表
        for (Long cid : cids) {
             count = brandMapper.insertCategoryBrand(cid, brand.getId());
                 if(count!=1){
                 throw new LyException(ExceptionEnums.BREAND_SAVE_ERROR);
             }
        }
    }


    @Override
    @Transactional
    public void updateBrand(Brand brand, List<Long> cids) {
        int count = brandMapper.updateByPrimaryKey(brand);
        if(count!=1){
            throw new LyException(ExceptionEnums.BREAND_SAVE_ERROR);
        }
        //删除tb_category_brand中所有该brand的信息
        brandMapper.deleteCategoryBrand(brand.getId());
        for (Long cid : cids) {
                count = brandMapper.insertCategoryBrand(cid, brand.getId());
                if(count!=1){
                    throw new LyException(ExceptionEnums.BREAND_UPDATA_ERROR);
                }
            }
    }

    @Override
    @Transactional
    public void deleteBrand(Long bid) {
        brandMapper.deleteByPrimaryKey(bid);
        //删除tb_category_brand中所有该brand的信息
        brandMapper.deleteCategoryBrand(bid);
    }

    @Override
    public Brand queryById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if(brand==null){
            throw new LyException(ExceptionEnums.BREAND_NOT_FOND);
        }
        return brand;
    }

    @Override
    public List<Brand> queryBrandByCid(Long cid) {
        List<Brand> brands = brandMapper.selectBrandByCid(cid);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnums.BREAND_NOT_FOND);
        }
        return brands;
    }

    @Override
    public List<Brand> queryByIds(List<Long> ids) {
        List<Brand> brands = brandMapper.selectByIdList(ids);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnums.BREAND_NOT_FOND);
        }
        return brands;
    }

}
